if (typeof ext == 'undefined') var ext = {};
ext.JConsole = {

id: 'org.hotot.jconsole',

name: 'Javascript Console',

description: 'A simple javscript console.',

version: '0.1.1',

author: 'Xu Zhen',

icon: "icon.png",

url: 'http://code.google.com/p/hotot-extensionz',

cmd_history: [],
cmd_history_idx: -1,

display: null,
command: null,

open_console:
function open_console() {
	var title = 'Javascript Console';
	var body = '<style>'+
			'#ext_hotot_jconsole_display{border:2px inset lightgrey;padding:0 10px;height:250px;overflow-y:auto;background:black;color:#D3D1D1}'+
			'#ext_hotot_jconsole_display li{padding-top:3px;word-wrap:break-word}'+
			'#ext_hotot_jconsole_input{width:100%}'+
			'#ext_hotot_jconsole_cmd{width:100%}'+
			'#ext_hotot_jconsole_input td[l]{width:1px;padding-left:5px}'+
			'#ext_hotot_jconsole_input td{border:none;padding:0}'+
		'</style>'+
		'<div>'+
		'<ul id="ext_hotot_jconsole_display">'+
		'</ul>'+
		'</div>'+
		'<div>'+
		'<table id="ext_hotot_jconsole_input">'+
		'<tr>'+
		'<td><input type="text" id="ext_hotot_jconsole_cmd" onkeydown="return (arguments[0].keyCode!=38 && arguments[0].keyCode!=40)"/></td>'+
		'<td l><input type="button" id="ext_hotot_jconsole_clear" value="Clear"/><td>'+
		'</tr>'+
		'</table>'+
		'</div>';
		
	ext.JConsole.option_dialog = widget.DialogManager.build_dialog(
		'#ext_hotot_jconsole_dialog', title, '', body, []);
	ext.JConsole.option_dialog.set_styles('header', {'display': 'none', 'height': '0'});
	ext.JConsole.option_dialog.set_styles('footer', {'display': 'none', 'height': '0'});
	ext.JConsole.option_dialog.resize(400, 350);

	ext.JConsole.display = $("#ext_hotot_jconsole_display");
	ext.JConsole.command = $("#ext_hotot_jconsole_cmd");

	ext.JConsole.command.keypress(ext.JConsole.on_keypress);
	ext.JConsole.command.keyup(ext.JConsole.on_keyup);
	$("#ext_hotot_jconsole_clear").click(ext.JConsole.clear_console);

	ext.JConsole.option_dialog.open();
},

clear_console:
function clear_console() {
	ext.JConsole.display.find("li").remove();
},

execute_command:
function execute_command() {
	var cmd = ext.JConsole.command.val();
	if (!cmd) {
		return;
	}
	var history = ext.JConsole.cmd_history;
	if (history[history.length - 1] != cmd) {
		history.push(cmd);
		if (history.length > 100) {
			history.splice(0, 1);
		}
		ext.JConsole.cmd_history_idx = history.length;
	}

	function add_new_line(text, reply, color) {
		var li = $(document.createElement("li"));
		if (reply) {
			li.html("<span>&gt;&gt;&nbsp;</span><span msg></span>");
		} else {
			li.html("<span msg></span>");
		}
		li.find("span[msg]").text("" + text).css("color", color);
		li.appendTo(ext.JConsole.display);

		var display = ext.JConsole.display.get(0);
		display.scrollTop = display.scrollHeight;
	}

	add_new_line(cmd);
	try {
		add_new_line(eval(cmd), true);
	} catch(ex) {
		add_new_line(ex.name + ": " + ex.message, true, "#EF2929");
	}
	ext.JConsole.command.val("");
},

on_keypress:
function on_keypress(event) {
	if (event.keyCode == 13) {
		// Enter
    	event.preventDefault();
		event.stopPropagation();
		ext.JConsole.execute_command();
	}
},

on_keyup:
function on_keyup(event) {
	if (event.keyCode == 38) {
		// Up
    	event.preventDefault();
		event.stopPropagation();
		var idx = ext.JConsole.cmd_history_idx;
		var history = ext.JConsole.cmd_history;
		if (idx > 0 && idx <= history.length) {
			ext.JConsole.cmd_history_idx -= 1;
			ext.JConsole.command.val(history[idx - 1]);
		}
	} else if (event.keyCode == 40) {
		// Down
    	event.preventDefault();
		event.stopPropagation();
		var idx = ext.JConsole.cmd_history_idx;
		var history = ext.JConsole.cmd_history;
		if (idx >= 0 && idx < history.length) {
			ext.JConsole.cmd_history_idx += 1;
			ext.JConsole.command.val(history[idx + 1] || "");
		}
	}
},

enable:
function enable() {
	ext.add_exts_menuitem('ext_btn_hotot_jconsole'
		, ext.JConsole.id+'/ic16_jconsole.png'
		, 'Javascript Console'
		, ext.JConsole.open_console);
},

disable:
function disable() {
	ext.remove_exts_menuitem('ext_btn_hotot_jconsole');
},

options:
function options() {
	ext.JConsole.open_console();
},

}

